package com.onesports.intelligent.k12.polarlight.service.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.BeanUtils;
import com.onesports.intelligent.k12.polarlight.annotation.DistributedLock;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.common.utils.DateUtils;
import com.onesports.intelligent.k12.polarlight.config.ApplicationProperties;
import com.onesports.intelligent.k12.polarlight.constant.CodeConstants;
import com.onesports.intelligent.k12.polarlight.constant.LockConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.CashPageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.CommissionDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.EvaluateDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.operation.PageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrgEvaluateDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrgPageOrderDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.report.FinanceQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.WxUserPurseChangeDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.wx.ServiceEvaluationADTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.ClassInfo;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.Course;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.SpellingClassDetails;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.Orders;
import com.onesports.intelligent.k12.polarlight.domain.entity.market.ServiceEvaluation;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.Organization;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.OrganizationCampus;
import com.onesports.intelligent.k12.polarlight.domain.entity.student.UserStudentRel;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.TotalDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.operation.CommissionPageVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.operation.StudentOrderVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.order.*;
import com.onesports.intelligent.k12.polarlight.domain.vo.product.WxOrderVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.FinanceVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.student.StudentCourseVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.wx.WXFtbOrderByIdVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.wx.WXOrderByIdVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.wx.WxFtbOrderVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.wx.WxOFVO;
import com.onesports.intelligent.k12.polarlight.enums.ClassInfoStatusEnum;
import com.onesports.intelligent.k12.polarlight.enums.OrderStatusEnum;
import com.onesports.intelligent.k12.polarlight.enums.PayTypeEnum;
import com.onesports.intelligent.k12.polarlight.enums.TradeTypeEnum;
import com.onesports.intelligent.k12.polarlight.mapper.course.ClassInfoMapper;
import com.onesports.intelligent.k12.polarlight.mapper.course.CourseMapper;
import com.onesports.intelligent.k12.polarlight.mapper.course.SpellingClassDetailsMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.FundsTradeBillMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.OrdersMapper;
import com.onesports.intelligent.k12.polarlight.mapper.market.ServiceEvaluationMapper;
import com.onesports.intelligent.k12.polarlight.mapper.organization.OrganizationCampusMapper;
import com.onesports.intelligent.k12.polarlight.mapper.organization.OrganizationMapper;
import com.onesports.intelligent.k12.polarlight.mapper.student.UserStudentRelMapper;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.sys.MessageNotifyService;
import com.onesports.intelligent.k12.polarlight.service.wx.WxUserPurseService;
import com.onesports.intelligent.k12.polarlight.util.BeanCopyUtil;
import com.onesports.intelligent.k12.polarlight.util.CalculateUtil;
import com.onesports.intelligent.k12.polarlight.util.CodeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 订单查询
 *
 * @author chenrl
 * @since 1.0.0 2022-03-16
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderService extends ServiceImpl<OrdersMapper, Orders> {

    private final OrdersMapper ordersMapper;

    private final ClassInfoMapper classInfoMapper;

    private final SpellingClassDetailsMapper spellingClassDetailsMapper;

    private final UserStudentRelMapper userStudentRelMapper;

    private final WxUserPurseService userPurseService;

    private final OrdersSubService ordersSubService;

    private final OrganizationMapper organizationMapper;

    private final OrganizationCampusMapper organizationCampusMapper;

    private final CourseMapper courseMapper;

    private final CodeUtils codeUtils;

    private final FundsTradeBillMapper fundsTradeBillMapper;

    private final ServiceEvaluationMapper serviceEvaluationMapper;

    private final MessageNotifyService messageNotifyService;

    /**
     * 运营端-订单分页查询
     * 订单分页查询
     *
     * @param pageOrderDTO
     */
    public IPage<OrderPageVO> selectPage(Page<OrderPageVO> orderPage, PageOrderDTO pageOrderDTO) {
        return ordersMapper.findPage(orderPage, pageOrderDTO);
    }

    public TotalDataVO<OrderPageVO> selectPageTotal(Page<OrderPageVO> OrderPage, PageOrderDTO pageOrderDTO) {
        TotalDataVO<OrderPageVO> orderPageVOTotalDataVO = new TotalDataVO<OrderPageVO>();
        IPage<OrderPageVO> page = ordersMapper.findPage(OrderPage, pageOrderDTO);
        OrderPageVO pageTotal = ordersMapper.findPageTotal(pageOrderDTO);
        orderPageVOTotalDataVO.setTotalData(pageTotal);
        orderPageVOTotalDataVO.setPage(page);
        return orderPageVOTotalDataVO;
    }

    /**
     * 根据id查询订单信息
     *
     * @return
     */
    public OrderByIdVO selectById(String orderId) {
        return ordersMapper.findById(orderId);
    }

    /**
     * 机构端-订单分页查询
     */
    public IPage<OrgOrderPageVO> selectOrgPage(Page<OrgOrderPageVO> OrderPage, OrgPageOrderDTO orgPageOrderDTO) {
        String organizationId = SecurityUtils.getOrganizationId();
        return ordersMapper.findOrgPage(OrderPage, orgPageOrderDTO, organizationId);
    }

    /**
     * 机构端-订单分页查询-TOTAL
     */
    public TotalDataVO<OrgOrderPageVO> selectOrgTotal(Page<OrgOrderPageVO> OrderPage, OrgPageOrderDTO orgPageOrderDTO) {
        String organizationId = SecurityUtils.getOrganizationId();
        TotalDataVO<OrgOrderPageVO> orgOrderPageVOTotalDataVO = new TotalDataVO<>();
        IPage<OrgOrderPageVO> orgPage = ordersMapper.findOrgPage(OrderPage, orgPageOrderDTO, organizationId);
        OrgOrderPageVO orgTotal = ordersMapper.findOrgTotal(OrderPage, orgPageOrderDTO, organizationId);
        orgOrderPageVOTotalDataVO.setPage(orgPage);
        orgOrderPageVOTotalDataVO.setTotalData(orgTotal);
        return orgOrderPageVOTotalDataVO;
    }


    /**
     * 学员课程订单列表
     *
     * @param studentId 学员第
     * @return List<StudentCourseVO>
     */
    public List<StudentCourseVO> courseList(String studentId, List<String> campusIds, List<String> classStatus) {
        return this.ordersMapper.courseList(studentId, campusIds, classStatus);
    }

    /**
     * 创建订单
     *
     * @return 订单
     */
    @DistributedLock(value = LockConstants.LOCK_CREATE_ORDER, keys = "#classId")
    @Transactional(rollbackFor = Exception.class)
    public Orders createOrder(String classId, String studentId, String courseScheduleDetailId, Boolean first) {
        UserStudentRel userStudentRel = userStudentRelMapper.selectOne(new QueryWrapper<UserStudentRel>().lambda().eq(UserStudentRel::getStudentId, studentId));
        ClassInfo classInfo = classInfoMapper.selectById(classId);
        SpellingClassDetails spellingClassDetails = spellingClassDetailsMapper.selectOne(new QueryWrapper<SpellingClassDetails>().lambda()
                .eq(SpellingClassDetails::getClassInfoId, classId));
        //冻结金额
        userPurseService.frozen(userStudentRel.getUserId(), spellingClassDetails.getPrice());
        // 生成待支付订单
        Orders order = new Orders();
        order.setFirstSpell(first);
        order.setStudentId(studentId);
        order.setPayer(userStudentRel.getUserId());
        order.setClassId(classId);
        order.setOrderAmount(spellingClassDetails.getPrice());
        order.setDiscountAmount(BigDecimal.ZERO);
        order.setActuallyAmount(spellingClassDetails.getPrice());
        order.setCampusId(classInfo.getCampusId());
        order.setOrderTime(new Date());
        order.setOrderStatus(OrderStatusEnum.WAIT.getValue());
        order.setOrganizationId(classInfo.getOrganizationId());
        order.setCoachId(classInfo.getCoachId());
        order.setHeadId(classInfo.getWxUserId());
        order.setProductId(classInfo.getProductId());
        order.setScheduleDetailId(courseScheduleDetailId);
        order.setCourseId(classInfo.getCourseId());
        order.setOrderNo(codeUtils.autoSerialNumber(CodeConstants.SF, 4));
        baseMapper.insert(order);

        //生成分账信息
        ordersSubService.addSubOrder(order);
        return order;
    }

    /**
     * 取消订单
     *
     * @param orderId 订单ID
     */
    @DistributedLock(value = LockConstants.LOCK_ORDER, keys = "#orderId")
    @Transactional(rollbackFor = Exception.class)
    public void cancelOrder(String orderId) {
        Orders order = baseMapper.selectById(orderId);
        if (order == null) {
            throw new BusinessException("取消订单失败，未获取到订单数据");
        }
        if (!order.getOrderStatus().equals(OrderStatusEnum.WAIT.getValue())) {
            throw new BusinessException("取消订单失败，订单" + OrderStatusEnum.typeOf(order.getOrderStatus()));
        }
        order.setOrderStatus(OrderStatusEnum.CANCEL.getValue());
        baseMapper.updateById(order);
        //解冻金额
        userPurseService.unfreeze(order.getPayer(), order.getActuallyAmount());
    }

    /**
     * 取消班级中待支付的订单
     *
     * @param classId
     */
    public void cancelOrderByClass(String classId) {
        List<Orders> ordersList = baseMapper.selectList(new LambdaQueryWrapper<Orders>().eq(Orders::getClassId, classId).eq(Orders::getOrderStatus, OrderStatusEnum.WAIT.getValue()));
        if (CollectionUtils.isNotEmpty(ordersList)) {
            ordersList.forEach(order -> cancelOrder(order.getId()));
        }
    }

    /**
     * 订单确认
     */
    @DistributedLock(value = LockConstants.LOCK_ORDER, keys = "#order.id")
    @Transactional(rollbackFor = Exception.class)
    public void confirm(Orders order,boolean firstSub) {
        if (order == null) {
            return ;
        }
        if (!order.getOrderStatus().equals(OrderStatusEnum.WAIT.getValue())) {
            return ;
        }
        order.setOrderStatus(OrderStatusEnum.ALREADY.getValue());
        order.setPayType(PayTypeEnum.SURPLUS.getValue());
        order.setPayTime(new Date());


        log.info("订单确认支付：{}", order.getOrderNo());
        //解冻金额
        userPurseService.unfreeze(order.getPayer(), order.getActuallyAmount());
        //课款缴费-金额变动
        WxUserPurseChangeDTO dto = WxUserPurseChangeDTO.builder()
                .tradeType(TradeTypeEnum.COURSE_PAY)
                .wxUserId(order.getPayer())
                .operateTime(new Date())
                .amount(order.getActuallyAmount())
                .orderNo(order.getOrderNo())
                .income(false)
                .build();
        userPurseService.inOrOutPurse(dto);
        //团长分佣/教练分佣
        if(!order.getFirstSpell()||firstSub){
            ordersSubService.subOrder(order);
            order.setCommission(true);
        }
        baseMapper.updateById(order);
        //消息通知
        messageNotifyService.autoPayRemind(order.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    public void commissionFirst(String classId){
        List<Orders> ordersList = baseMapper.selectList(new LambdaQueryWrapper<Orders>().eq(Orders::getClassId, classId)
                .eq(Orders::getOrderStatus, OrderStatusEnum.ALREADY.getValue())
                .eq(Orders::getFirstSpell,true)
                .eq(Orders::getCommission,false)
        );
        if (CollectionUtils.isNotEmpty(ordersList)) {
            ordersList.forEach(order->{
                ordersSubService.subOrder(order);
                order.setCommission(true);
                baseMapper.updateById(order);
            });
        }
    }


    /**
     * 运营端-分佣记录-条件查询带分页
     *
     * @param commissionDTO
     * @return
     */
    public IPage<CommissionPageVO> selectPageByOperate(Page<CommissionPageVO> OrderPage, CommissionDTO commissionDTO) {
        return ordersMapper.findCommissionPage(OrderPage, commissionDTO);
    }

    /**
     * 运营端-服务评价
     *
     * @param orderPageVOPage
     * @param evaluateDTO
     * @return
     */
    public IPage<OrderEvaluatePageVO> selectEvaluatePage(Page<OrderEvaluatePageVO> orderPageVOPage, EvaluateDTO evaluateDTO) {
        return ordersMapper.findEvaluatePage(orderPageVOPage, evaluateDTO);
    }

    /**
     * 根据用户id查询订单信息
     *
     * @return
     */
    public List<WxOFVO> getAllOrder() {
        String uesrId = SecurityUtils.getAccountId();
        List<WxOrderVO> wxOrderVOS = ordersMapper.wxFindOrder(uesrId);
        wxOrderVOS.forEach(i -> {
            List<ServiceEvaluation> serviceEvaluations = serviceEvaluationMapper.selectList(new QueryWrapper<ServiceEvaluation>().lambda()
                    .eq(ServiceEvaluation::getOrdersId, i.getOrderId()));
            if (serviceEvaluations.size() > 0) {
                i.setIsEvaluation(true);
                i.setEvaluationLevel(serviceEvaluations.get(0).getEvaluationLevel());
                i.setContent(serviceEvaluations.get(0).getContent());
            } else {
                i.setIsEvaluation(false);
            }
        });
        List<WxFtbOrderVO> wxFtbOrderVOS = ordersMapper.wxFtbOrder(uesrId);
        List<WxOFVO> wx = new ArrayList<>();
        for (WxOrderVO wxOrderVO : wxOrderVOS) {
            WxOFVO wxOFVO = new WxOFVO();
            wxOFVO.setOperateTime(wxOrderVO.getOrderTime());
            wxOFVO.setWxOrderVO(wxOrderVO);
            wx.add(wxOFVO);
        }
        for (WxFtbOrderVO wxFtbOrderVO : wxFtbOrderVOS) {
            WxOFVO wxOFVO = new WxOFVO();
            wxOFVO.setOperateTime(wxFtbOrderVO.getOperateTime());
            wxOFVO.setWxFtbOrderVO(wxFtbOrderVO);
            wx.add(wxOFVO);
        }

        return wx.stream()
                .sorted(Comparator.comparing(WxOFVO::getOperateTime).reversed())
                .collect(Collectors.toList());
    }

    /**
     * 根据用户id查询消费订单信息
     *
     * @return
     */
    public List<WxOrderVO> getWxOrder() {
        String uesrId = SecurityUtils.getAccountId();
        List<WxOrderVO> wxOrderVOS = ordersMapper.wxFindOrder(uesrId);
        wxOrderVOS.forEach(i -> {
            List<ServiceEvaluation> serviceEvaluations = serviceEvaluationMapper.selectList(new QueryWrapper<ServiceEvaluation>().lambda()
                    .eq(ServiceEvaluation::getOrdersId, i.getOrderId()));
            if (serviceEvaluations.size() > 0) {
                i.setIsEvaluation(true);
                i.setEvaluationLevel(serviceEvaluations.get(0).getEvaluationLevel());
                i.setContent(serviceEvaluations.get(0).getContent());
            } else {
                i.setIsEvaluation(false);
            }
        });
        return wxOrderVOS;
    }

    /**
     * 根据用户id查询充值订单信息
     *
     * @return
     */
    public List<WxFtbOrderVO> getFtbOrder() {
        String uesrId = SecurityUtils.getAccountId();
        return ordersMapper.wxFtbOrder(uesrId);
    }

    /**
     * 查询运营团队-校区信息
     *
     * @return
     */
    public OrganAndCampusVO selectByOAndC() {
        List<Organization> organizations = organizationMapper.selectList(new QueryWrapper<Organization>().lambda()
                .eq(Organization::getStatus, true));
        List<OrganVO> organVOS = BeanCopyUtil.copyListProperties(organizations, OrganVO::new);
        List<OrganizationCampus> organizationCampuses = organizationCampusMapper.selectList(new QueryWrapper<OrganizationCampus>().lambda()
                .eq(OrganizationCampus::getStatus, true));
        List<CampusAVO> campusAVOS = BeanCopyUtil.copyListProperties(organizationCampuses, CampusAVO::new);
        OrganAndCampusVO organAndCampusVO = new OrganAndCampusVO();
        organAndCampusVO.setOrgan(organVOS);
        organAndCampusVO.setCam(campusAVOS);
        return organAndCampusVO;
    }

    /**
     * 查询页下拉-团队校区课程
     *
     * @return
     */
    public OrganAndCampusVO selectByOAndCAndC() {
        List<Organization> organizations = organizationMapper.selectList(new QueryWrapper<Organization>().lambda()
                .eq(Organization::getStatus, true));
        List<OrganVO> organVOS = BeanCopyUtil.copyListProperties(organizations, OrganVO::new);
        List<OrganizationCampus> organizationCampuses = organizationCampusMapper.selectList(new QueryWrapper<OrganizationCampus>().lambda()
                .eq(OrganizationCampus::getStatus, true));
        List<CampusAVO> campusAVOS = BeanCopyUtil.copyListProperties(organizationCampuses, CampusAVO::new);
        List<Course> courses = courseMapper.selectList(null);
        List<CourseAVO> courseAVOS = BeanCopyUtil.copyListProperties(courses, CourseAVO::new);
        OrganAndCampusVO organAndCampusVO = new OrganAndCampusVO();
        organAndCampusVO.setOrgan(organVOS);
        organAndCampusVO.setCam(campusAVOS);
        organAndCampusVO.setCou(courseAVOS);
        return organAndCampusVO;
    }

    /**
     * 提现记录
     *
     * @param cashOrderPageVOPage
     * @param cashPageOrderDTO
     * @return
     */
    public IPage<CashOrderPageVO> selectCashPage(Page<CashOrderPageVO> cashOrderPageVOPage, CashPageOrderDTO cashPageOrderDTO) {
        return fundsTradeBillMapper.findCashPage(cashOrderPageVOPage, cashPageOrderDTO);
    }

    /**
     * 机构端-教务管理-服务评价
     *
     * @param orgOrderEvaluatePageVOPage
     * @param orgEvaluateDTO
     * @return
     */
    public IPage<OrgOrderEvaluatePageVO> selectOrgEvaluatePage(Page<OrgOrderEvaluatePageVO> orgOrderEvaluatePageVOPage, OrgEvaluateDTO orgEvaluateDTO) {
        String organizationId = SecurityUtils.getOrganizationId();
        return ordersMapper.findOrdEvaluatePage(orgOrderEvaluatePageVOPage, orgEvaluateDTO, organizationId);
    }

    /**
     * 根据学员id查询消费订单信息
     *
     * @return
     */
    public List<StudentOrderVO> getStudentOrder(String studentId) {
        String organizationId = SecurityUtils.getOrganizationId();

        return ordersMapper.wxFindStudentOrderByOrg(studentId,organizationId);
    }

    public List<StudentOrderVO> getStudentOrder1(String studentId) {
        return ordersMapper.wxFindStudentOrder(studentId);
    }
    /**
     * 财务统计
     *
     * @return
     */
    public TotalDataVO<FinanceVO> selectFinancePage(FinanceQueryDTO dto, String organizationId) {
        IPage<FinanceVO> page = ordersMapper.selectFinancePage(dto.getPager(), dto, organizationId);
        FinanceVO totalData = ordersMapper.selectFinanceSumValue(dto, organizationId);
        TotalDataVO<FinanceVO> totalDataVO = new TotalDataVO<>();
        totalDataVO.setTotalData(totalData);
        totalDataVO.setPage(page);
        return totalDataVO;
    }

    /**
     * 财务统计导出数据
     *
     * @return
     */
    public List<FinanceVO> financeExport(FinanceQueryDTO dto, String organizationId) {

        List<FinanceVO> list = ordersMapper.selectFinancePage(dto, organizationId);
        FinanceVO totalData = ordersMapper.selectFinanceSumValue(dto, organizationId);
        if (null != totalData) {
            totalData.setOrderNo("合计：");
            list.add(totalData);
        }
        return list;
    }

    public List<Orders> waitPayByClassId(String classId) {
        return this.ordersMapper.selectList(new LambdaQueryWrapper<Orders>()
                .eq(Orders::getOrderStatus, OrderStatusEnum.WAIT.getValue())
                .eq(Orders::getClassId, classId));
    }

    /**
     * 添加服务评价
     *
     * @return
     */
    public void setServiceEvaluation(ServiceEvaluationADTO dto) {
        List<Orders> orders = ordersMapper.selectList(new QueryWrapper<Orders>().lambda()
                .eq(Orders::getId, dto.getOrdersId()));
        if (orders.size() < 1) {
            throw new BusinessException("请输入正确的订单编号！");
        }
        ServiceEvaluation serviceEvaluation = new ServiceEvaluation();
        Orders orders1 = orders.get(0);
        BeanUtils.copyProperties(orders1, serviceEvaluation, "createdDate");
        BeanUtils.copyProperties(dto, serviceEvaluation);
        serviceEvaluationMapper.insert(serviceEvaluation);
    }

    /**
     * 做流的测试
     *
     * @param dto
     */
    public void test(ServiceEvaluationADTO dto) {

    }

    /**
     * 小程序端-根据订单id查询订单信息
     *
     * @param orderId
     * @return
     */
    public WXOrderByIdVO getOrderAll(String orderId) {
        return ordersMapper.selectWXorderById(orderId);
    }

    public WXFtbOrderByIdVO getFtbOrderById(String tradeId) {
        return ordersMapper.selectWXFtborderById(tradeId);
    }

    /**
     * 获取已考勤的订单
     *
     * @return
     */
    public List<Orders> getAttendanceOrders(Date payTime) {
        return ordersMapper.getAttendanceOrders(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, payTime));
    }

    public List<OrderPageVO> selectExprotTotal(PageOrderDTO dto) {
        List<OrderPageVO> records = ordersMapper.findPage(new Page<>(1, Integer.MAX_VALUE), dto).getRecords();
        OrderPageVO pageTotal = ordersMapper.findPageTotal(dto);
        if(null!=pageTotal){
            pageTotal.setOrganizationName("合计：");
            records.add(pageTotal);
        }
        if(CollectionUtils.isNotEmpty(records)){
            return records;
        }
        return null;
    }

    public List<OrgOrderPageVO> selectOrgTotalExport(OrgPageOrderDTO dto) {
        String organizationId = SecurityUtils.getOrganizationId();
        List<OrgOrderPageVO> records = ordersMapper.findOrgPage(new Page<>(1, Integer.MAX_VALUE), dto, organizationId).getRecords();
        OrgOrderPageVO orgTotal = ordersMapper.findOrgTotal(new Page<>(1, Integer.MAX_VALUE), dto, organizationId);
        if(null!=orgTotal){
            orgTotal.setCampusName("合计：");
            records.add(orgTotal);
        }
        if(CollectionUtils.isNotEmpty(records)){
            return records;
        }
        return null;
    }
}
